package com.mxt.moviedemo.service.impl;

import com.mxt.moviedemo.controller.UserController;
import com.mxt.moviedemo.dao.UserMapper;
import com.mxt.moviedemo.entity.PageRequest;
import com.mxt.moviedemo.entity.PageResult;
import com.mxt.moviedemo.entity.User;
import com.mxt.moviedemo.service.UserService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Resource
    private UserMapper userMapper;


    @Override
    public User loginForPhone(String phone, String password) {
//        System.out.println("phone: " + phone + ", pwd: " + password);
        return userMapper.loginForPhone(phone, password);
    }

    @Override
    public User loginForEmail(String email, String password) {
        return userMapper.loginForEmail(email, password);
    }

    @Override
    public User loginByUsernameAndPassword(String uname, String password, String role) {
        System.out.println("uname" + uname + ", password" + password);
        return userMapper.loginByUsernameAndPassword(uname, password, role);
    }

    @Override
    public int registerUser(User user) {
        // 获取下一个可用的 uid
        int nextUid = getNextUid();
        user.setUidAndUuid(nextUid);


        user.setPassword(user.getPassword());

        return userMapper.registerUser(user);
    }

    @Override
    public int getNextUid() {
        return userMapper.getNextUid();
    }

    @Override
    public User getUserByUsername(String uname) {
        System.out.println(uname);
        return userMapper.getUserByUsername(uname);
    }

    @Override
    public int updateUser(User user) {
        System.out.println("修改后的数据");
        return userMapper.updateUser(user);
    }
    @Override
    @Transactional
    public int updateTicketUsername(String uname, String newName,User user) {
        // 更新用户表中的用户名
        int rows = userMapper.updateUser(user);

        if (rows > 0) {
            // 获取新用户名
            String newUsername = user.getUname();

            // 判断旧用户名和新用户名是否不同，如果不同才更新订单表
            if (!uname.equals(newUsername)) {
                // 更新订单表中的用户名
                int ticketRows = userMapper.updateTicketUsername(uname,newName);
                System.out.println("更新订单表用户名影响行数: " + ticketRows);
            } else {
                System.out.println("新旧用户名相同，无需更新订单表");
            }
        }
        return rows;
    }

    @Override
    public boolean recharge(String username, double amount) {
        return userMapper.recharge(username,amount);
    }

    @Override
    public PageResult<User> getUsersByPage(PageRequest pageRequest) {
        List<User> users = userMapper.getUsersByPage(pageRequest);
        long total = userMapper.getTotalUsers();
        return new PageResult<>(users, total);
    }

    @Override
    public List<User> searchUsersByName(String uname) {
        return userMapper.searchUsersByName(uname);
    }

    @Override
    public boolean deleteUserByUuid(int uuid) {
        return userMapper.deleteUserByUuid(uuid);
    }

    @Override
    public int getUserIdByUsername(String uname) {
        User user = userMapper.getUserByUsername(uname);
        if (user != null) {
            return user.getUuid();
        }
        return 0;
    }


}